package com.koocloud.electroplatemanage.service.impl;

import cn.hutool.core.lang.Validator;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koocloud.electroplatemanage.auth.mapper.IUserDao;
import com.koocloud.electroplatemanage.common.pojo.ResponseTemplate;
import com.koocloud.electroplatemanage.mapper.StatisticalFormMapper;
import com.koocloud.electroplatemanage.pojo.User;
import com.koocloud.electroplatemanage.service.StatisticalFormService;
import com.koocloud.electroplatemanage.utils.ArithmeticUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.util.StringUtils;

import static com.koocloud.electroplatemanage.constant.IncomePartJudgeSqlFieldConstant.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

/**
 * @program: Information_platform
 * @description: 报表实现层
 * @author: xxc
 * @create: 2020-10-21 13:42
 */
@Service
@Transactional(rollbackFor=Exception.class)
public class StatisticalFormServiceImpl implements StatisticalFormService {
    @Resource
    private StatisticalFormMapper statisticalFormMapper;
    @Resource
    private IUserDao iUserDao;
    /**
     *@Description: 整体按月完成情况
     *@Param: [year]
     *@return: com.koocloud.electroplatemanage.common.pojo.ResponseTemplate
     *@Author: xxc
     *@date: 2020/10/21
     */
    @Override
    public ResponseTemplate selectAllByMonthStatus(String year) {
        try {
            //创建返回前端实体List
            List<LinkedHashMap> list = new ArrayList<>();
            if(StringUtils.isEmpty(year)) {//判断年份是否为空，空则取当年年份
                Calendar cal = Calendar.getInstance();
                //取当年年份
                year = String.valueOf(cal.get(Calendar.YEAR));
            }
            //设置开始时间，结束时间
            String startDate = "0";
            String endDate = "0";
            // 项数  总数量 总价格 总工时   合计
            Double yearTotalProjectNum = 0.0;
            Double yearSumProjectNum= 0.0;
            BigDecimal yearTotalProjectPrice = new BigDecimal(0);
            BigDecimal yearTotalProjectWorkTime = new BigDecimal(0);
            //第一个循环代表12个月
            for (int i = 1; i <= 12; i++) {
                if (i == 1) {
                    startDate = String.valueOf(Integer.parseInt(year) - 1) + "-12-26 00:00:00";
                    endDate = year + "-01-25 23:59:59";
                } else {
                    startDate = year + "-" + String.format("%02d",(i - 1)) + "-26 00:00:00";
                    endDate = year + "-" + String.format("%02d",(i )) + "-25 23:59:59";
                }
                Double totalProjectNum = 0.0; //个人按月完成项数
                Double sumProjectNum= 0.0;//个人按月件数
                BigDecimal totalProjectPrice = new BigDecimal(0);////个人按月含税总价
                BigDecimal totalProjectWorkTime = new BigDecimal(0);//个人按月完成工时
                //单月查询
                List<LinkedHashMap> listData = statisticalFormMapper.selectAllByMonthStatus(startDate,endDate);
                for (LinkedHashMap linkedHashMap:listData){
                    totalProjectNum = Double.valueOf(StringUtils.isEmpty(linkedHashMap.get("terms"))? "0":String.valueOf(linkedHashMap.get("terms")));
                    sumProjectNum = Double.valueOf(StringUtils.isEmpty(linkedHashMap.get("num"))? "0":String.valueOf(linkedHashMap.get("num")));
                    totalProjectPrice = new BigDecimal(StringUtils.isEmpty(linkedHashMap.get("totalPrice"))? "0":String.valueOf(linkedHashMap.get("totalPrice")));
                    totalProjectWorkTime = new BigDecimal(StringUtils.isEmpty(linkedHashMap.get("totalTime"))? "0":String.valueOf(linkedHashMap.get("totalTime")));
                }
                // 项数  总数量 总价格 总工时
                yearTotalProjectNum += totalProjectNum;
                yearSumProjectNum += sumProjectNum;
                yearTotalProjectPrice = ArithmeticUtils.add(String.valueOf(yearTotalProjectPrice),String.valueOf(totalProjectPrice));
                yearTotalProjectWorkTime = ArithmeticUtils.add(String.valueOf(yearTotalProjectWorkTime),String.valueOf(totalProjectWorkTime));
                //单月
                LinkedHashMap LinkedMap = new LinkedHashMap();
                LinkedMap.put("month",i);
                LinkedMap.put("totalProjectNum",totalProjectNum);
                LinkedMap.put("totalProjectWorkTime",totalProjectWorkTime);
                LinkedMap.put("totalProjectPrice",totalProjectPrice);
                LinkedMap.put("sumProjectNum",sumProjectNum);
                list.add(LinkedMap);
            }
            //合计
            LinkedHashMap LinkedMapTemp = new LinkedHashMap();
            LinkedMapTemp.put("month", TOTAL);
            LinkedMapTemp.put("totalProjectNum",yearTotalProjectNum);
            LinkedMapTemp.put("totalProjectWorkTime",yearTotalProjectWorkTime);
            LinkedMapTemp.put("totalProjectPrice",yearTotalProjectPrice);
            LinkedMapTemp.put("sumProjectNum",yearSumProjectNum);
            list.add(LinkedMapTemp);

            return ResponseTemplate.builder().code(0).message("查询成功").count(Long.valueOf(list.size()) ).data(list).build();
        }catch (Exception e){
            //事务手动回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            //遇到异常，返回异常原因
            e.printStackTrace();
            return ResponseTemplate.builder().code(1).message(e.getMessage()).count(0l).build();
        }

    }
    /**
     *@Description: 个人按月完成项数
     *@Param: [year]
     *@return: com.koocloud.electroplatemanage.common.pojo.ResponseTemplate
     *@Author: xxc
     *@date: 2020/10/21
     */
    @Override
    public ResponseTemplate selectByMonthPersonTerms(String year,String page ,String limit) {
        //校验分页参数是否为纯数字 并且page和limit不都等于0
        if (Validator.isNumber(page) && Validator.isNumber(limit) && !("0".equals(page) && "0".equals(limit))) {
            try {
                PageHelper.startPage(Integer.valueOf(page), Integer.valueOf(limit));
                //创建返回前端实体List
                List<LinkedHashMap> list = new ArrayList<>();

                if(StringUtils.isEmpty(year)) {//判断年份是否为空，空则取当年年份
                    Calendar cal = Calendar.getInstance();
                    //取当年年份
                    year = String.valueOf(cal.get(Calendar.YEAR));
                }
                //设置开始时间，结束时间
                String startDate = "0";
                String endDate = "0";
                //查询人数
                List<LinkedHashMap> listPeopleData = statisticalFormMapper.selectByMonthPersonqWorkNum();

                for (LinkedHashMap listPeopleDatum : listPeopleData) {
                    //每个月份个人
                    String  operator =String.valueOf(listPeopleDatum.get("operator"));
                    StringBuilder operators = new StringBuilder();
                    if (operator.contains("、")){
                        String[] splits = operator.split("、");
                        for (int j = 0; j < splits.length; j++) {
                            User user = iUserDao.selectByJobNum(splits[j]);
                            if (j == 0){
                                operators.append(user.getFullname());
                            }else {
                                operators.append("、").append(user.getFullname());
                            }
                        }
                    }else {
                        User user = iUserDao.selectByJobNum(operator);
                        operators.append(user.getFullname());
                    }
                    //第一个循环代表12个月
                    //个人每月
                    LinkedHashMap LinkedMap = new LinkedHashMap();
                    LinkedMap.put("name",operators);
                    Double yearTotalProjectNum = 0.0;
                    for (int i = 1; i <= 12; i++) {
                        if (i == 1) {
                            startDate = String.valueOf(Integer.parseInt(year) - 1) + "-12-26 00:00:00";
                            endDate = year + "-01-25 23:59:59";
                        } else {
                            startDate = year + "-" + String.format("%02d",(i-1)) + "-26 00:00:00";
                            endDate = year + "-" + String.format("%02d",i) + "-25 23:59:59";
                        }
                        Double totalProjectNum = 0.0; //个人按月完成项数
                        //Double sumProjectNum= 0.0;//个人按月件数
                        //BigDecimal totalProjectPrice = new BigDecimal(0);////个人按月含税总价
                        //BigDecimal totalProjectWorkTime = new BigDecimal(0);//个人按月完成工时

                        //根据个人按月完成项数、件数、含税总价、工时汇总
                        List<LinkedHashMap> listData = statisticalFormMapper.selectByMonthPersonStatus(startDate, endDate, operator);
                        for (LinkedHashMap linkedHashMap:listData){
                            totalProjectNum = Double.valueOf(StringUtils.isEmpty(linkedHashMap.get("terms"))? "0":String.valueOf(linkedHashMap.get("terms")));
                            //sumProjectNum = Double.valueOf(StringUtils.isEmpty(linkedHashMap.get("num"))? "0":String.valueOf(linkedHashMap.get("num")));
                            //totalProjectPrice = new BigDecimal(StringUtils.isEmpty(linkedHashMap.get("totalPrice"))? "0":String.valueOf(linkedHashMap.get("totalPrice")));
                            //totalProjectWorkTime = new BigDecimal(StringUtils.isEmpty(linkedHashMap.get("totalTime"))? "0":String.valueOf(linkedHashMap.get("totalTime")));
                        }
                        // 项数
                        yearTotalProjectNum += totalProjectNum;
                        //单月
                        LinkedMap.put(i,totalProjectNum);

                    }
                    LinkedMap.put(TOTAL,yearTotalProjectNum);
                    list.add(LinkedMap);
                }
                //构造分页实体
                PageInfo<LinkedHashMap> info = new PageInfo<>(list);
                if (info.getTotal() > 0) {
                    //查询到数据
                    return ResponseTemplate.builder().code(0).message("查询成功").count((long) info.getSize()).data(info.getList()).build();
                } else {
                    //没有查询到数据
                    return ResponseTemplate.builder().code(1).message("没有查到对应数据").count(0l).build();
                }
            }catch (Exception e){
                //事务手动回滚
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                //遇到异常，返回异常原因
                e.printStackTrace();
                return ResponseTemplate.builder().code(1).message(e.getMessage()).count(0l).build();
            }
        } else {
            return ResponseTemplate.builder().code(1).message("分页参数错误").count(0l).build();
        }
    }
    /**
     *@Description: 个人按月完成数量
     *@Param: [year]
     *@return: com.koocloud.electroplatemanage.common.pojo.ResponseTemplate
     *@Author: xxc
     *@date: 2020/10/21
     */
    @Override
    public ResponseTemplate selectByMonthPersonNums(String year,String page ,String limit) {
        //校验分页参数是否为纯数字 并且page和limit不都等于0
        if (Validator.isNumber(page) && Validator.isNumber(limit) && !("0".equals(page) && "0".equals(limit))) {
            try {                PageHelper.startPage(Integer.valueOf(page), Integer.valueOf(limit));
                //创建返回前端实体List
                List<LinkedHashMap> list = new ArrayList<>();

                if(StringUtils.isEmpty(year)) {//判断年份是否为空，空则取当年年份
                    Calendar cal = Calendar.getInstance();
                    //取当年年份
                    year = String.valueOf(cal.get(Calendar.YEAR));
                }
                //设置开始时间，结束时间
                String startDate = "0";
                String endDate = "0";
                //查询人数
                List<LinkedHashMap> listPeopleData = statisticalFormMapper.selectByMonthPersonqWorkNum();

                for (LinkedHashMap listPeopleDatum : listPeopleData) {
                    //每个月份个人
                    String  operator =String.valueOf(listPeopleDatum.get("operator"));
                    //通过工号赋值名称
                    StringBuilder operators = new StringBuilder();
                    if (operator.contains("、")){
                        String[] splits = operator.split("、");
                        for (int j = 0; j < splits.length; j++) {
                            User user = iUserDao.selectByJobNum(splits[j]);
                            if (j == 0){
                                operators.append(user.getFullname());
                            }else {
                                operators.append("、").append(user.getFullname());
                            }
                        }
                    }else {
                        User user = iUserDao.selectByJobNum(operator);
                        operators.append(user.getFullname());
                    }
                    //第一个循环代表12个月
                    //个人每月
                    LinkedHashMap LinkedMap = new LinkedHashMap();
                    LinkedMap.put("name",operators);
                    Double yearSumProjectNum= 0.0;
                    for (int i = 1; i <= 12; i++) {
                        if (i == 1) {
                            startDate = String.valueOf(Integer.parseInt(year) - 1) + "-12-26 00:00:00";
                            endDate = year + "-01-25 23:59:59";
                        } else {
                            startDate = year + "-" + String.format("%02d",(i-1)) + "-26 00:00:00";
                            endDate = year + "-" + String.format("%02d",i) + "-25 23:59:59";
                        }
                        Double sumProjectNum= 0.0;//个人按月件数
                        //根据个人按月完成项数、件数、含税总价、工时汇总
                        List<LinkedHashMap> listData = statisticalFormMapper.selectByMonthPersonStatus(startDate, endDate, operator);
                        for (LinkedHashMap linkedHashMap:listData){
                            sumProjectNum = Double.valueOf(StringUtils.isEmpty(linkedHashMap.get("num"))? "0":String.valueOf(linkedHashMap.get("num")));
                        }
                        // 件数
                        yearSumProjectNum += sumProjectNum;
                        //单月
                        LinkedMap.put(i,sumProjectNum);
                    }
                    LinkedMap.put(TOTAL,yearSumProjectNum);
                    list.add(LinkedMap);
                }
                //构造分页实体
                PageInfo<LinkedHashMap> info = new PageInfo<>(list);
                if (info.getTotal() > 0) {
                    //查询到数据
                    return ResponseTemplate.builder().code(0).message("查询成功").count((long) info.getSize()).data(info.getList()).build();
                } else {
                    //没有查询到数据
                    return ResponseTemplate.builder().code(1).message("没有查到对应数据").count(0l).build();
                }
            }catch (Exception e){
                //事务手动回滚
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                //遇到异常，返回异常原因
                e.printStackTrace();
                return ResponseTemplate.builder().code(1).message(e.getMessage()).count(0l).build();
            }
        } else {
            return ResponseTemplate.builder().code(1).message("分页参数错误").count(0l).build();
        }
    }
    /**
     *@Description: 个人按月完成含税总价
     *@Param: [year]
     *@return: com.koocloud.electroplatemanage.common.pojo.ResponseTemplate
     *@Author: xxc
     *@date: 2020/10/21
     */
    @Override
    public ResponseTemplate selectByMonthPersonTotalPrice(String year,String page ,String limit) {
        //校验分页参数是否为纯数字 并且page和limit不都等于0
        if (Validator.isNumber(page) && Validator.isNumber(limit) && !("0".equals(page) && "0".equals(limit))) {
            try {
                PageHelper.startPage(Integer.valueOf(page), Integer.valueOf(limit));
                //创建返回前端实体List
                List<LinkedHashMap> list = new ArrayList<>();

                if(StringUtils.isEmpty(year)) {//判断年份是否为空，空则取当年年份
                    Calendar cal = Calendar.getInstance();
                    //取当年年份
                    year = String.valueOf(cal.get(Calendar.YEAR));
                }
                //设置开始时间，结束时间
                String startDate = "0";
                String endDate = "0";
                //查询人数
                List<LinkedHashMap> listPeopleData = statisticalFormMapper.selectByMonthPersonqWorkNum();

                for (LinkedHashMap listPeopleDatum : listPeopleData) {
                    //每个月份个人
                    String  operator =String.valueOf(listPeopleDatum.get("operator"));
                    StringBuilder operators = new StringBuilder();
                    if (operator.contains("、")){
                        String[] splits = operator.split("、");
                        for (int j = 0; j < splits.length; j++) {
                            User user = iUserDao.selectByJobNum(splits[j]);
                            if (j == 0){
                                operators.append(user.getFullname());
                            }else {
                                operators.append("、").append(user.getFullname());
                            }
                        }
                    }else {
                        User user = iUserDao.selectByJobNum(operator);
                        operators.append(user.getFullname());
                    }
                    //第一个循环代表12个月
                    //个人每月
                    LinkedHashMap LinkedMap = new LinkedHashMap();
                    LinkedMap.put("name",operators);
                    //long sum = 0;
                    BigDecimal yearTotalProjectPrice = new BigDecimal(0);////个人按月含税总价
                    for (int i = 1; i <= 12; i++) {
                        long start = System.currentTimeMillis();
                        if (i == 1) {
                            startDate = String.valueOf(Integer.parseInt(year) - 1) + "-12-26 00:00:00";
                            endDate = year + "-01-25 23:59:59";
                        } else {
                            startDate = year + "-" + String.format("%02d",(i-1)) + "-26 00:00:00";
                            endDate = year + "-" + String.format("%02d",i) + "-25 23:59:59";
                        }
                        BigDecimal totalProjectPrice = new BigDecimal(0);////个人按月含税总价

                        //根据个人按月完成项数、件数、含税总价、工时汇总
                        List<LinkedHashMap> listData = statisticalFormMapper.selectByMonthPersonStatus(startDate, endDate, operator);
                        for (LinkedHashMap linkedHashMap:listData){
                            totalProjectPrice = new BigDecimal(StringUtils.isEmpty(linkedHashMap.get("totalPrice"))? "0":String.valueOf(linkedHashMap.get("totalPrice")));
                        }
                        // 金额
                        yearTotalProjectPrice = ArithmeticUtils.add(String.valueOf(yearTotalProjectPrice),String.valueOf(totalProjectPrice));
                        //单月
                        LinkedMap.put(i,totalProjectPrice);
                        long end = System.currentTimeMillis();
                       // sum+=end-start;
                        //System.out.println(sum);

                    }
                    LinkedMap.put(TOTAL,yearTotalProjectPrice);
                    list.add(LinkedMap);
                }
                //构造分页实体
                PageInfo<LinkedHashMap> info = new PageInfo<>(list);
                if (info.getTotal() > 0) {
                    //查询到数据
                    return ResponseTemplate.builder().code(0).message("查询成功").count((long) info.getSize()).data(info.getList()).build();
                } else {
                    //没有查询到数据
                    return ResponseTemplate.builder().code(1).message("没有查到对应数据").count(0l).build();
                }
            }catch (Exception e){
                //事务手动回滚
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                //遇到异常，返回异常原因
                e.printStackTrace();
                return ResponseTemplate.builder().code(1).message(e.getMessage()).count(0l).build();
            }
        } else {
            return ResponseTemplate.builder().code(1).message("分页参数错误").count(0l).build();
        }
    }
    /**
     *@Description: 个人按月完成总工时
     *@Param: [year]
     *@return: com.koocloud.electroplatemanage.common.pojo.ResponseTemplate
     *@Author: xxc
     *@date: 2020/10/21
     */
    @Override
    public ResponseTemplate selectByMonthPersonTotalHours(String year,String page ,String limit) {
        //校验分页参数是否为纯数字 并且page和limit不都等于0
        if (Validator.isNumber(page) && Validator.isNumber(limit) && !("0".equals(page) && "0".equals(limit))) {
            try {
                PageHelper.startPage(Integer.valueOf(page), Integer.valueOf(limit));
                //创建返回前端实体List
                List<LinkedHashMap> list = new ArrayList<>();

                if(StringUtils.isEmpty(year)) {//判断年份是否为空，空则取当年年份
                    Calendar cal = Calendar.getInstance();
                    //取当年年份
                    year = String.valueOf(cal.get(Calendar.YEAR));
                }
                //设置开始时间，结束时间
                String startDate = "0";
                String endDate = "0";
                //查询人数
                List<LinkedHashMap> listPeopleData = statisticalFormMapper.selectByMonthPersonqWorkNum();

                for (LinkedHashMap listPeopleDatum : listPeopleData) {
                    //每个月份个人
                    String  operator =String.valueOf(listPeopleDatum.get("operator"));
                    StringBuilder operators = new StringBuilder();
                    if (operator.contains("、")){
                        String[] splits = operator.split("、");
                        for (int j = 0; j < splits.length; j++) {
                            User user = iUserDao.selectByJobNum(splits[j]);
                            if (j == 0){
                                operators.append(user.getFullname());
                            }else {
                                operators.append("、").append(user.getFullname());
                            }
                        }
                    }else {
                        User user = iUserDao.selectByJobNum(operator);
                        operators.append(user.getFullname());
                    }
                    //第一个循环代表12个月
                    //个人每月
                    LinkedHashMap LinkedMap = new LinkedHashMap();
                    LinkedMap.put("name",operators);
                    BigDecimal yearTotalProjectWorkTime = new BigDecimal(0);
                    for (int i = 1; i <= 12; i++) {
                        if (i == 1) {
                            startDate = String.valueOf(Integer.parseInt(year) - 1) + "-12-26 00:00:00";
                            endDate = year + "-01-25 23:59:59";
                        } else {
                            startDate = year + "-" + String.format("%02d",(i-1)) + "-26 00:00:00";
                            endDate = year + "-" + String.format("%02d",i) + "-25 23:59:59";
                        }
                        BigDecimal totalProjectWorkTime = new BigDecimal(0);//个人按月完成工时

                        //根据个人按月完成项数、件数、含税总价、工时汇总
                        List<LinkedHashMap> listData = statisticalFormMapper.selectByMonthPersonStatus(startDate, endDate, operator);
                        for (LinkedHashMap linkedHashMap:listData){
                            totalProjectWorkTime = new BigDecimal(StringUtils.isEmpty(linkedHashMap.get("totalTime"))? "0":String.valueOf(linkedHashMap.get("totalTime")));
                        }
                        // 工时
                        yearTotalProjectWorkTime = ArithmeticUtils.add(String.valueOf(yearTotalProjectWorkTime),String.valueOf(totalProjectWorkTime));
                        //单月
                        LinkedMap.put(i,totalProjectWorkTime);

                    }
                    LinkedMap.put(TOTAL,yearTotalProjectWorkTime);
                    list.add(LinkedMap);
                }
                //构造分页实体
                PageInfo<LinkedHashMap> info = new PageInfo<>(list);
                if (info.getTotal() > 0) {
                    //查询到数据
                    return ResponseTemplate.builder().code(0).message("查询成功").count((long) info.getSize()).data(info.getList()).build();
                } else {
                    //没有查询到数据
                    return ResponseTemplate.builder().code(1).message("没有查到对应数据").count(0l).build();
                }
            }catch (Exception e){
                //事务手动回滚
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                //遇到异常，返回异常原因
                e.printStackTrace();
                return ResponseTemplate.builder().code(1).message(e.getMessage()).count(0l).build();
            }
        } else {
            return ResponseTemplate.builder().code(1).message("分页参数错误").count(0l).build();
        }
    }
    /**
     *@Description: 单月完成情况
     *@Param: [year]
     *@return: com.koocloud.electroplatemanage.common.pojo.ResponseTemplate
     *@Author: xxc
     *@date: 2020/10/21
     */
    @Override
    public ResponseTemplate selectByMonthStatus(String page ,String limit,String year,String month) {
        //校验分页参数是否为纯数字 并且page和limit不都等于0
        if (Validator.isNumber(page) && Validator.isNumber(limit) && !("0".equals(page) && "0".equals(limit))) {
            try {
                PageHelper.startPage(Integer.valueOf(page), Integer.valueOf(limit));
                //创建返回前端实体List
                List<LinkedHashMap> list = new ArrayList<>();
                if(StringUtils.isEmpty(year)) {//判断年份是否为空，空则取当年年份
                    Calendar cal = Calendar.getInstance();
                    //取当年年份
                    year = String.valueOf(cal.get(Calendar.YEAR));

                }
                //设置开始时间，结束时间
                String startDate = "0";
                String endDate = "0";
                if (Integer.valueOf(month) == 1) {
                    startDate = String.valueOf(Integer.parseInt(year) - 1) + "-12-26 00:00:00";
                    endDate = year + "-01-25 23:59:59";
                } else {
                    startDate = year + "-" + String.format("%02d",(Integer.valueOf(month)-1)) + "-26 00:00:00";
                    endDate = year + "-" + String.format("%02d",Integer.valueOf(month)) + "-25 23:59:59";
                }
                //查询人数
                List<LinkedHashMap> listPeopleData = statisticalFormMapper.selectByMonthPersonqWorkNum();

                Double monthTotalProjectNum = 0.0; //合计个人按月完成项数
                Double monthSumProjectNum = 0.0;//合计个人按月件数
                BigDecimal monthTotalProjectPrice = new BigDecimal(0);////合计个人按月含税总价
                BigDecimal monthTotalProjectWorkTime = new BigDecimal(0);//合计个人按月完成工时
                LinkedHashMap totalLinkedMap = new LinkedHashMap();
                totalLinkedMap.put("name", TOTAL);
                for (LinkedHashMap listPeopleDatum : listPeopleData) {
                    //每个月份个人
                    String operator = String.valueOf(listPeopleDatum.get("operator"));
                    StringBuilder operators = new StringBuilder();
                    if (operator.contains("、")){
                        String[] splits = operator.split("、");
                        for (int j = 0; j < splits.length; j++) {
                            User user = iUserDao.selectByJobNum(splits[j]);
                            if (j == 0){
                                operators.append(user.getFullname());
                            }else {
                                operators.append("、").append(user.getFullname());
                            }
                        }
                    }else {
                        User user = iUserDao.selectByJobNum(operator);
                        operators.append(user.getFullname());
                    }
                    //个人每月
                    LinkedHashMap LinkedMap = new LinkedHashMap();
                    LinkedMap.put("name", operators);
                    //LinkedMap.put("month",month);
                    Double totalProjectNum = 0.0; //个人按月完成项数
                    Double sumProjectNum = 0.0;//个人按月件数
                    BigDecimal totalProjectPrice = new BigDecimal(0);////个人按月含税总价
                    BigDecimal totalProjectWorkTime = new BigDecimal(0);//个人按月完成工时
                    //根据个人按月完成项数、件数、含税总价、工时汇总
                    List<LinkedHashMap> listData = statisticalFormMapper.selectByMonthPersonStatus(startDate, endDate, operator);
                    for (LinkedHashMap linkedHashMap:listData){
                        totalProjectNum = Double.valueOf(StringUtils.isEmpty(linkedHashMap.get("terms"))? "0":String.valueOf(linkedHashMap.get("terms")));
                        sumProjectNum = Double.valueOf(StringUtils.isEmpty(linkedHashMap.get("num"))? "0":String.valueOf(linkedHashMap.get("num")));
                        totalProjectPrice = new BigDecimal(StringUtils.isEmpty(linkedHashMap.get("totalPrice"))? "0":String.valueOf(linkedHashMap.get("totalPrice")));
                        totalProjectWorkTime = new BigDecimal(StringUtils.isEmpty(linkedHashMap.get("totalTime"))? "0":String.valueOf(linkedHashMap.get("totalTime")));
                    }
                    // 项数  总数量 总价格 总工时
                    monthTotalProjectNum += totalProjectNum;
                    monthSumProjectNum += sumProjectNum;
                    monthTotalProjectPrice = ArithmeticUtils.add(String.valueOf(monthTotalProjectPrice),String.valueOf(totalProjectPrice));
                    monthTotalProjectWorkTime = ArithmeticUtils.add(String.valueOf(monthTotalProjectPrice),String.valueOf(totalProjectWorkTime));
                    //单月
                    LinkedMap.put("totalProjectNum",totalProjectNum);
                    LinkedMap.put("totalProjectWorkTime",totalProjectWorkTime);
                    LinkedMap.put("totalProjectPrice",totalProjectPrice);
                    LinkedMap.put("sumProjectNum",sumProjectNum);
                    list.add(LinkedMap);
                }
                totalLinkedMap.put("totalProjectNum",monthTotalProjectNum);
                totalLinkedMap.put("totalProjectWorkTime",monthTotalProjectWorkTime);
                totalLinkedMap.put("totalProjectPrice",monthTotalProjectPrice);
                totalLinkedMap.put("sumProjectNum",monthSumProjectNum);
                list.add(totalLinkedMap);
                //构造分页实体
                PageInfo<LinkedHashMap> info = new PageInfo<>(list);
                if (info.getTotal() > 0) {
                    //查询到数据
                    return ResponseTemplate.builder().code(0).message("查询成功").count((long) info.getSize()).data(info.getList()).build();
                } else {
                    //没有查询到数据
                    return ResponseTemplate.builder().code(1).message("没有查到对应数据").count(0l).build();
                }
            }catch (Exception e){
                //事务手动回滚
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                //遇到异常，返回异常原因
                e.printStackTrace();
                return ResponseTemplate.builder().code(1).message(e.getMessage()).count(0l).build();
            }
        } else {
            return ResponseTemplate.builder().code(1).message("分页参数错误").count(0l).build();
        }
    }
}
